import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: '首页',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

export const asyncRoutes = [
  {
    path: '/book',
    name: 'book',
    component: Layout,
    redirect: '/book/create',
    meta: { title: '商品管理', icon: 'documentation', roles: ['admin', 'aditor'] },
    children: [
      {
        name: 'bookCreate',
        path: '/book/create',
        component: () => import('@/views/book/create.vue'),
        meta: { title: '创建商品', icon: 'edit', roles: ['admin'] }
      },
      {
        name: 'bookList',
        path: '/book/list',
        component: () => import('@/views/book/list.vue'),
        meta: { title: '商品列表', icon: 'list', roles: ['editor'] }
      },
      {
        name: 'bookBanner',
        path: '/book/banner',
        component: () => import('@/views/book/banner.vue'),
        meta: { title: 'banner', icon: 'list', roles: ['editor'] }
      },
      {
        name: 'bookUser',
        path: '/book/user',
        component: () => import('@/views/book/user.vue'),
        meta: { title: '用户列表', icon: 'list', roles: ['editor'] }
      },
      {
        name: 'bookOrder',
        path: '/book/order',
        component: () => import('@/views/book/order.vue'),
        meta: { title: '查询订单', icon: 'search', roles: ['editor'] }
      },
      {
        name: 'bookEdit',
        path: '/book/edit',
        component: () => import('@/views/book/edit'),
        hidden: true,
        meta: { title: '编辑商品', icon: 'edit', roles: ['admin'], activeMenu: '/book/list' }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
